<?php

namespace app\api\model\cps;


//抖音
use app\api\model\ExtendConsume;
use app\api\model\UserConsumeLog;
use app\common\model\User;
use app\shopapi\model\ExtendConsumeShop;
use fast\Http;
use fast\Random;
use pangolin\sdk\http\PopHttpClient;

require_once ROOT_PATH.'extend/dy-sdk/src/http/PopHttpClient.php';
require_once ROOT_PATH.'extend/dy-sdk/src/api/ProductSearchRequest.php';
require_once ROOT_PATH.'extend/dy-sdk/src/api/ProductLinkRequest.php';
require_once ROOT_PATH.'extend/dy-sdk/src/api/OrderSearchRequest.php';

class Dy
{

    public $appid = "5553385";

    public $roleld = '222900';

    public $securitykey = "j_8iCH20ez-o01SLasLG1bmN9EY-2BYT8DiY_s5hONM=";

    public $url = "https://ecom.pangolin-sdk-toutiao.com/";


    //团购商品列表
    public function goods($category_id,$sort_by,$order_by,$cursor,$longitude,$latitude,$keyword)
    {
        $client=$this->getPddClient();
        $request=new \pangolin\sdk\api\ProductSearchRequest();

        if ($category_id){
            $request->setCategoryId($category_id);
        }
        if ($sort_by){
            $request->setSortBy($sort_by);
        }
        if ($order_by){
            $request->setOrderBy($order_by);
        }
        if ($cursor){
            $request->setCursor($cursor);
        }
        if ($longitude){
            $request->setLongitude($longitude);
        }
        if ($latitude){
            $request->setLatitude($latitude);
        }
        if ($keyword){
            $request->setKeyword($keyword);
        }
        $res=$client->syncInvoke($request);

        $data = json_decode($res->getBody());
        if ($data->code==0){
            $result = $data->data;
            if (isset($result->product_list)){
                foreach ($result->product_list as $v){
                    $commission_amount = $v->commission_amount/100;
                    $v->wawa_commission = round($commission_amount*0.56,2);
                }
            } else {
                $result->product_list = [];
            }


            return $result;
        } else {
            throw new \Exception($data->desc);
        }

    }

    //商品转链
    public function link($command,$user_id,$mobile)
    {
        $client=$this->getPddClient();
        $request=new \pangolin\sdk\api\ProductLinkRequest();

        $request->setCommand($command);
        $request->setCommandExternalInfo($mobile.'_'.$user_id);
        $request->setNeedQrCode(true);
        $request->setNeedShareCommand(true);
        $res=$client->syncInvoke($request);
        //dump($res);die;
        $data = json_decode($res->getBody());
        if ($data->code==0){
            return $data->data;
        } else {
            throw new \Exception($data->desc);
        }
    }

    //查询未结算的订单
    public function unliquidated()
    {
        $lists = UserConsumeLog::where(['cps_type'=>6,'consume_type'=>2,'is_to_account'=>0])->field('pay_time')->select();

        foreach ($lists as $k=>$v){
            $start = $v['pay_time']-3600;
            $end = $v['pay_time'];
            $res = $this->order(1,$start,$end);
        }
    }


    //订单列表
    public function order($type=0,$start='',$end='')
    {
        $client=$this->getPddClient();
        $request=new \pangolin\sdk\api\OrderSearchRequest();

        $begin_time = time()-3600;
        $end_time = time();
        if ($type==1){
            $begin_time = $start;
            $end_time = $end;
        }
        $request->setPayTimeBegin($begin_time);
        $request->setPayTimeEnd($end_time);
        $res=$client->syncInvoke($request);

        $data = json_decode($res->getBody());
        if ($data->code==0){

            $list = $data->data;

            if (isset($list->order_list)){
                foreach ($list->order_list as $k=>$v){

                    $subUnionId = explode('_',$v->command_external_info);

                    $user = User::get(['id'=>$subUnionId[1]]);
                    if ($user){
                        //增加消费记录
                        $consume_log_info = UserConsumeLog::get([ 'user_id'=>$user->id, 'order_no'=>$v->order_id]);
                        $estimateFee = $v->commission_amount/100;
                        $product_list = $v->product_list;

                        $commission = $estimateFee*0.56;
                        if (!$consume_log_info){
                            $consume_log_model = new UserConsumeLog();
                            $consume_log_data = [
                                'user_id'=>$user->id,
                                'title'=>$product_list[0]->name,
                                'shop_id'=>$v->poi_info->poi_id,
                                'shop_name'=>$v->poi_info->name,
                                'image'=>$product_list[0]->img,
                                'order_no'=>$v->order_id,
                                'pay_time'=>$v->pay_time,
                                'pay_price'=>$v->pay_amount/100,
                                'consume_type'=>2,
                                'cps_type'=>6,
                                'goods_id'=>$product_list[0]->product_id,
                                'commission'=>$commission,
                                'total_commission'=>$estimateFee,
                                'order_status_name'=>'已付款'
                            ];

                            User::update(['is_buy'=>1],['id'=>$user->id]);

                            $user_model = new User();
                            $user_model->upgrade($user->id);
                            $consume_log_model->save($consume_log_data);

                            UserConsumeLog::lockMemberConsumeCalculate($user->id,$consume_log_model->total_commission,$consume_log_model->consume_id,0);
                            UserConsumeLog::newrecommendShopConsumeCalculate($user->id,0,$consume_log_model->total_commission,$consume_log_model->consume_id,2);
                        } else {
                            if ($v->settle_time>0&&$consume_log_info->is_to_account==0){
                                //发放消费记录的收益
                                UserConsumeLog::sendCommission($consume_log_info->order_no);
                            } else {
                                if ($v->status==2&&$consume_log_info->is_refund==0){
                                    $consume_log_info->commission = 0;
                                    $consume_log_info->order_status_name = '退款成功';
                                    $consume_log_info->is_refund = 1;
                                    $consume_log_info->save();
                                    ExtendConsume::update(['commission'=>0],['consume_log_id'=>$consume_log_info->consume_id]);
                                    ExtendConsumeShop::update(['commission'=>0],['consume_log_id'=>$consume_log_info->consume_id]);
                                }

                            }

                        }
                    }

                }
            }

            return $list;
        } else {
            throw new \Exception($data->desc);
        }
    }

    public function getPddClient()
    {
        return new PopHttpClient($this->appid,$this->securitykey);
    }


}